<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Start Cloud Development With MEAN on AWS1</title>
        <meta name="description" content="The Dead-Simple Step-by-Step Tutorial for Web Developers to Getting Up and Running with Node.JS, Express, Jade, and MongoDB on Amazon EC2 on AWS EC2">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    <body>
 
        <!-- WRAP -->
        <div >

            <!-- BASE CONTAINER -->
            <div class="container">

          

                <!-- CONTENT & SIDEBAR WRAP -->
                <div style="" class="contentsidebarwrap clearfix">

                    <!-- CONTENT -->
                    <article  style=" " class="content subpage web tutorial">
                        <h1>
                            Start Cloud Development with Node.js, Express, Jade and MongoDB Development on Amazon EC2
                        </h1>

                        <h2>

                        </h2>

                        <h3 class="subtitle">
                            Simple Step-by-Step Guide for
                            Web application Developers to Getting Up and Running with
                            Node.JS, Express, Jade, and MongoDB on Amazon EC2. <br/>
                            Set up the full stack and have a webpage running in 30 minutes. Make it talk to your DB in another 30.
                        </h3>

                        <p class="byline">Originally By <a href="http://cwbuecheler.com/" target="_blank">Christopher Buecheler</a></p>
                        <p class="byline"> Ported to AWS EC2 By <a href="http://facweb.northseattle.edu/zwu/" target="_blank">Paul Zhou Wu</a></p>


                        <p style="padding:15px; background:#EEE;"><a href="https://github.com/cwbuecheler/node-tutorial-for-frontend-devs" target="_blank">You can find/fork the entire sample project on GitHub</a></p>



                        <h3>Introduction</h3>
                        <p>
                            Chris did a wonderful job with his excellent <a href="http://http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/"> tutorial </a> back in 
                            2013. Today, Cloud Computing is the hottest thing on the IT planet. Doing things on cloud may be a necessity for any new development at present.  In this
                            article, Paul followed Chris' original work frame to show how the things can be done on Amazon EC2. Current article keeps large
                            part of the original texts, but with significant technical changes that are needed on the new platform. 

                        </p>
                        <p>
                            There are approximately one hundred million tutorials on the web for
                            getting a "Hello, World!" app running with Node.js. This is great! It's
                            especially great if your goal is to greet the world and then give up on
                            your web career and go spend the rest of your life as, like, a jockey or
                            something. That doesn't really describe most of us, so we go looking for
                            more tutorials.
                        </p>

                        <p>
                            In  experience, the "next level" tutorials out there seem about 30
                            levels further along. We go from "Hello, World!" to building out an entire
                            blogging system with comments,  which is also great, but a lot of times
                            those tutorials assume the reader has done a whole bunch of intermediate
                            fiddling, and they often drop a bunch of big functions on you all at once.
                            We tend to learn best by making lots of smaller, intermediate steps, and We 
                            don't think we are the only one.
                        </p>

                        <p>
                            We are not the only one, right?
                        </p>

                        <p>
                            Well, <a href="https://www.youtube.com/watch?v=z6ODMDtG6-I" target="blank">good news, everyone!</a>
                            We've done the fiddling and read a bunch of tutorials and shouted at our
                            command prompt until things finally worked. We have a web project up and
                            running which uses Node.JS, the Express framework, the Jade HTML
                            pre-processor, and MongoDB for data. We can read to and write from the DB.
                            From there, the sky's the limit.
                        </p>

                        <p>
                            Here's the deal:  We are  going to show you how to get all of this stuff set
                            up. We'll be assuming that you're a web developer who knows
                            HTML5/CSS3/JavaScript well enough that we don't have to explain those. If
                            that's you, then this should be a solid primer.
                        </p>

                        <p>
                            Your app will look pretty, it will connect to a DB, it'll get some
                            results, and it'll do stuff with those results. Then for kicks we'll also
                            make it save data to the DB. Through it all, We will explain what the code
                            does, and how to write it, instead of just giving you massive functions to
                            stare at. We'll go from nothing even installed, to a DB-driven web app
                            written in a language you fully understand, and the foundation necessary
                            to build additional functionality into your app. And we'll do it in about
                            60 minutes of installation and coding time. Is that awesome? We submit that
                            it is.
                        </p>

                        <p>
                            Let's go.
                        </p>

                        <h3>Part I – 15~30 minutes of Setting Up </h3>

                        <p>
                            If you're really starting from scratch, then getting everything up and
                            running takes a little bit of time. None of it is difficult. 
                        </p>

                        <h4>Step 1 – Create a free EC2 Instance with <a href="https://bitnami.com/stack/mean/cloud/amazon"> Bitnami Mean Image </a> </h4>

                        <p>
                            If you have  AWS account, you can go to <a href="#login"> here to start </a>. Otherwise, you
                            can create an AWS account, which provides some Web Services for free up to 12 months following your AWS sign-up date. In the free services,
                            you can create  EC2 (Virtual Server) “micro-instance” at no cost for one year. Let us go to 
                            <a href="https://aws.amazon.com/free/, target='_blank'">https://aws.amazon.com/free/ </a>. 
                            Follow the directions provided on the AWS web site to set up a user account. Although it is free, 
                            but you do have to provide your credit card. Like any other 'free' things, Amazon wants your businesses later.
                        </p>

                        <p>
                            <a name="login"></a> Log into the <a href="aws.amazon.com" target="_blank"> AWS Console </a> web page login using your Amazon userid and password. 
                            Follow this <a href="https://northseattle.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=62b7ffcf-5dbc-499f-a633-ba2979d2123c" target="_blank">video </a>
                            to create your EC2 instance with MEAN stack from Bitnami. The video also shows how to use PUTTY on Windows to connect the instance.
                            If you use Linux/MacOS X, you probably know how to use ssh (easier than PUTTY on Windows) to connect the instance. By the way, in this article, we assume
                            the public DNS for our EC2 instance is <i> ec2-52-24-196-1099.us-west-2.compute.amazonaws.com</i>. You should find yours in your EC2's dashboard. 
                        </P>





                        <h4>Step 2 – Create an Express Project</h4>

                        <p>
                            We're going to use Express and Jade, but not the Stylus CSS preprocessor
                            (which people often use in this stack). We're just going to use straight CSS
                            for right now. We have to use Jade or another templating engine to gain access
                            to our Node/Express-based data. Jade's not hard to learn if you already know
                            HTML, just remember that you really have to pay attention to indentation or
                            things will go badly wrong. If you want a templating engine with true HTML
                            syntax, you could use <a href="https://www.npmjs.com/package/ejs" target="_blank">EJS</a>
                            instead (<strong>note: We cannot provide support for this tutorial
                                if you choose to use anything other than Jade</strong>).
                        </p>

                        <p>
                            A quick
                            note on indentation: everything in this tutorial has been normalized to 4-space tabs, <em>even code that was auto-generated
                                with 2-space tabs</em>. If you want to use two or three spaces, or 
                            actual tabs
                            (which is usually my preference), that's just fine by me. But, again, 
                            YOUR INDENTATION MUST BE CONSISTENT or Jade will throw errors. Not just 
                            "all tabs" or "all spaces" but you must be very, very careful to make 
                            sure that everything is indented the exact right amount. For example, in
                            Jade, the following:
                        </p>

                        <pre style="" class="prettyprint prettyprinted"><span class="pln">body
    h1
    ul
        li</span></pre>

                        <p>
                            ... is VERY different from:
                        </p>

                        <pre style="" class="prettyprint prettyprinted"><span class="pln">body
    h1
        ul
            li</span></pre>

                        <p>
                            (that second piece of code would nest the UL inside of the H1 ... which you obviously don't want).
                        </p>

                        <p>
                            Anyway, the following  <pre>bitnami@ip-172-31-20-96:~$</pre> represents the home directory on your EC2 instance. 

                        You use this folder or  wherever you're storing your node apps. Open a PUTTY/ssh session to the EC2 instance,  type this:
                        </p>

                        <h5>Command: bitnami@ip-172-31-20-96:~$</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln"></span><span class="pun"></span><span class="pln">bitnami@ip-172-31-20-96:~$</span><span class="pun"></span><span class="pln"> express proj1</span></pre>

                        <p>
                            Hit enter and watch it go. You'll see something like this:
                        </p>


                        <pre style="" class="prettyprint prettyprinted">bitnami@ip-172-31-20-96:~$ express proj1

   create : proj1
   create : proj1/package.json
   create : proj1/app.js
   create : proj1/public
   create : proj1/public/javascripts
   create : proj1/public/images
   create : proj1/public/stylesheets
   create : proj1/public/stylesheets/style.css
   create : proj1/routes
   create : proj1/routes/index.js
   create : proj1/routes/users.js
   create : proj1/views
   create : proj1/views/index.jade
   create : proj1/views/layout.jade
   create : proj1/views/error.jade
   create : proj1/bin
   create : proj1/bin/www

   install dependencies:
     $ cd proj1 && npm install

   run the app:
     $ DEBUG=proj1:* npm start
                        </pre>



                        <p>
                            It's going to print out a ton of stuff, and we have to appreciate it can do so
                            much tedious work for us. Once NPM has run its course,
                            you should have a node_modules directory which contains all of our
                            dependencies for this tutorial.
                        </p>

                        <p>
                            You now have a fully-functioning app ready and waiting to run. Still in your proj1 directory, type this:
                        </p>

                        <h5>Command: bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted">bitnami@ip-172-31-20-96:~$ DEBUG=proj1:* npm start</pre>

                        <p>You'll get this: </p>



                        <h5>Node Console</h5>
                        <pre style="" class="prettyprint prettyprinted">> proj1@0.0.0 start /home/bitnami/myapps/proj1
> node ./bin/www

[Error: /opt/bitnami/nodejs/lib/node_modules/mongodb/node_modules/bson/build/Release/bson.node: invalid ELF header]
js-bson: Failed to load c++ bson extension, using pure JS version
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
  proj1:server Listening on port 3000 +0ms
                        </pre>
                        <p>
                            <B> Don't worry about the Error now. </B>  Open a new ssh/PUTTY session to the EC2 instance and use your 
                            favorite editor (we use nano), do the following
                        </p>
                        <h5>Command: bitnami@ip-172-31-20-96:~$</h5>
                        <pre style="" class="prettyprint prettyprinted">bitnami@ip-172-31-20-96:~$ nano /opt/bitnami/apache2/conf/httpd.conf </pre>

                        Add the following lines to the end of the file:
                        <pre>
ProxyRequests Off
ProxyVia Off

ProxyPass /proj1 http://127.0.0.1:3000
ProxyPassReverse /proj1 http://127.0.0.1:3000

                        </pre>

                        <p> Save the file and execute the following command: </p>
                        <pre>
    bitnami@ip-172-31-20-96:~/proj1$ sudo /opt/bitnami/ctlscript.sh restart apache
                        </pre>
                        <p>
                            Everything working? Awesome! Open a browser and head for
                            <i> http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1</i> (<b>Warning, you must use your own EC2 url</b>)
                            where you will see a welcome to Express page.
                        </p>

                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot1.png" alt="Welcome to Express Page"></p>

                        <p>
                            You are now running your own Node JS webserver, which is behind Apache webserver, with the Express engine
                            and Jade HTML preprocessor installed. Not so tough, huh?
                        </p>

                        <h3>Part 2 – OK, Fine, let's do "Hello, World!"</h3>

                        <p>
                            Fire up your favorite text editor. Point it at your proj1 directory and open app.js. This is kind
                            of the heart of your, well, app. Not a big surprise there. Here's a
                            breakdown of what you're going to see:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'express'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'path'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> favicon </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'serve-favicon'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'morgan'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> cookieParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'cookie-parser'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> bodyParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'body-parser'</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">var</span><span class="pln"> routes </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'./routes/index'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> users </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'./routes/users'</span><span class="pun">);</span></pre>

                        <p>
                            This creates a bunch of basic JavaScript variables and ties them to
                            certain packages, dependencies, node functionality, and routes. Routes are
                            kind of like a combination of models and controllers in this setup –
                            they direct traffic and also contain some programming logic (you can
                            establish a more traditional MVC architecture with Express if you like.
                            That's outside of the scope of this article). Back when we set up this
                            project, Express created all of this stuff for us. We're going to totally
                            ignore the user route for now and just work in the top level route
                            (controlled by bitnami@ip-172-31-20-96:~$ ~/proj1/routes/index.js).
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> app </span><span class="pun">=</span><span class="pln"> express</span><span class="pun">();</span></pre>

                        <p>
                            This one's important. It instantiates Express and assigns our app variable
                            to it. The next section uses this variable to configure a bunch of Express
                            stuff.
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">// view engine setup</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'views'</span><span class="pun">,</span><span class="pln"> path</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="pln">__dirname</span><span class="pun">,</span><span class="pln"> </span><span class="str">'views'</span><span class="pun">));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'view engine'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'jade'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// uncomment after placing your favicon in /public</span><span class="pln">
</span><span class="com">//app.use(favicon(__dirname + '/public/favicon.ico'));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">logger</span><span class="pun">(</span><span class="str">'dev'</span><span class="pun">));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">bodyParser</span><span class="pun">.</span><span class="pln">json</span><span class="pun">());</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">bodyParser</span><span class="pun">.</span><span class="pln">urlencoded</span><span class="pun">({</span><span class="pln"> extended</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">false</span><span class="pln"> </span><span class="pun">}));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">cookieParser</span><span class="pun">());</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">express</span><span class="pun">.</span><span class="kwd">static</span><span class="pun">(</span><span class="pln">path</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="pln">__dirname</span><span class="pun">,</span><span class="pln"> </span><span class="str">'public'</span><span class="pun">)));</span><span class="pln">

app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="pln"> routes</span><span class="pun">);</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/users'</span><span class="pun">,</span><span class="pln"> users</span><span class="pun">);</span></pre>

                        <p>
                            This tells the app where to find its views, what engine to
                            use to render those views (Jade), and calls a few methods to get things up
                            and running. Note also that the final line is telling Express to serve
                            static objects from the /public/ dir, but to make them actually seem like
                            they're coming from the top level (it also does this with the views
                            directory). For example, the images directory is
                            ~/proj1/public/images … but it is accessed at <i>
                                http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1/images </i>
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">/// catch 404 and forwarding to error handler</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> err </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Error</span><span class="pun">(</span><span class="str">'Not Found'</span><span class="pun">);</span><span class="pln">
    err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">=</span><span class="pln"> </span><span class="lit">404</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">next</span><span class="pun">(</span><span class="pln">err</span><span class="pun">);</span><span class="pln">
</span><span class="pun">});</span><span class="pln">

</span><span class="com">/// error handlers</span><span class="pln">

</span><span class="com">// development error handler</span><span class="pln">
</span><span class="com">// will print stacktrace</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">app</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'env'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">===</span><span class="pln"> </span><span class="str">'development'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        res</span><span class="pun">.</span><span class="pln">status</span><span class="pun">(</span><span class="pln">err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">||</span><span class="pln"> </span><span class="lit">500</span><span class="pun">);</span><span class="pln">
        res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'error'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            message</span><span class="pun">:</span><span class="pln"> err</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln">
            error</span><span class="pun">:</span><span class="pln"> err
        </span><span class="pun">});</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">// production error handler</span><span class="pln">
</span><span class="com">// no stacktraces leaked to user</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">status</span><span class="pun">(</span><span class="pln">err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">||</span><span class="pln"> </span><span class="lit">500</span><span class="pun">);</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'error'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        message</span><span class="pun">:</span><span class="pln"> err</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln">
        error</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            These are error handlers for development and production (and 404's). We're
                            not really worrying about the different between those two right now, but
                            basically if your app is in development mode, your errors will give you
                            more information. Obviously you don't want to print a stack trace out on a
                            production site that anyone on the web can see.
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">module</span><span class="pun">.</span><span class="pln">exports </span><span class="pun">=</span><span class="pln"> app</span><span class="pun">;</span></pre>

                        <p>
                            A core part of Node is that basically all modules export an object which
                            can easily be called elsewhere in the code. Our master app exports its app
                            object.
                        </p>

                        <p>
                            Now then, let's make stuff. We're not going to just stick "Hello, World!"
                            on our index page. Instead we're going to use this as an opportunity to
                            learn a bit more about routes and to take a look at how Jade works for
                            putting pages together.
                        </p>

                        <p>
                            We're going to start by adding a new app.use directive to app.js. Find
                            the section that looks like this:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="pln"> routes</span><span class="pun">);</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/users'</span><span class="pun">,</span><span class="pln"> users</span><span class="pun">);</span></pre>

                        <p>
                            This directives are telling Express what route files to use. Now, normally
                            we would advocate setting up separate route files for different parts of your
                            app. For example, the users route file might contain routes for adding
                            users, deleting them, updating them, and so forth, while a new route file
                            called "locations" might handle adding, editing, deleting and displaying
                            location data (in an app for which that was required). In this case, to
                            keep things simple, we're going to do everything in the index router. That
                            means you can completely ignore the /users line.
                        </p>

                        <p>
                            Remember that the Express scaffolding already defined the "routes"
                            variable and pointed it at the index router. We're going to add a
                            "helloworld" method to that router which will render a different page than
                            the default. In your text editor, open up your routes folder,
                            find index.js, and open it. It will look like this:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/routes/index.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'express'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> router </span><span class="pun">=</span><span class="pln"> express</span><span class="pun">.</span><span class="typ">Router</span><span class="pun">();</span><span class="pln">

</span><span class="com">/* GET home page. */</span><span class="pln">
router</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'index'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Express'</span><span class="pln"> </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span><span class="pln">

</span><span class="kwd">module</span><span class="pun">.</span><span class="pln">exports </span><span class="pun">=</span><span class="pln"> router</span><span class="pun">;</span></pre>

                        <p>
                            Pretty sparse, right? Basically we're requiring our Express functionality,
                            then attaching a "router" variable to Express's router method, then using
                            that method when an attempt is made to HTTP get the top level directory of
                            our website. Finally we export our router function back to our app.
                        </p>
                        <p>
                            We can easily clone that <i>get</i> function for another page, so let's do that.
                            At the bottom of the file, just <strong>above</strong> the module.exports
                            line, add this code:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/routes/index.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">/* GET Hello World page. */</span><span class="pln">
router</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/helloworld'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'helloworld'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Hello, World!'</span><span class="pln"> </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            That's all it takes to handle routing the URI, but we don't have any
                            actual page for res.render to … render. That's where Jade comes in.
                            Open up your views folder, and then go ahead and open index.jade. Before
                            you do anything else, <strong>save it as helloworld.jade</strong>.
                        </p>

                        <p>
                            Now take a look at the code:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/views/helloworld.jade</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">extends</span><span class="pln"> layout

block content
    h1</span><span class="pun">=</span><span class="pln"> title
    p </span><span class="typ">Welcome</span><span class="pln"> to </span><span class="com">#{title}</span></pre>

                        <p>
                            This is pretty straightforward. It uses ("extends") the file layout.jade
                            as a template, and then within the content block defined in the layout
                            file, it sticks a header and a paragraph. Note the use of the "title"
                            variable which we set above, in our index.js route. This means we don't
                            even have to change the text at all in order for it to show different
                            stuff from the home page. But let's change it anyway to:
                        </p>

                        <pre style="" class="prettyprint prettyprinted"><span class="pln">p </span><span class="typ">Hello</span><span class="pun">,</span><span class="pln"> </span><span class="typ">World</span><span class="pun">!</span><span class="pln"> </span><span class="typ">Welcome</span><span class="pln"> to </span><span class="com">#{title}</span></pre>

                        <p>
                            Save the file, go to your command prompt, ctrl-c to kill your server if
                            it's already running, and then type:
                        </p>

                        <h5>Command bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">npm start</span></pre>

                        <p>
                            In order to restart the server. By the way, this seems a good time to
                            mention: changes to Jade templates do not require a server restart, but
                            basically whenever you change a js file, such as app.js or the route
                            files, you'll need to restart to see changes.
                        </p>

                        <p>
                            SO … with the server restarted, navigate to <i>
                                http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1/helloworld </i>
                            and enjoy the completely asinine text that gets displayed:
                        </p>

                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot2.png" alt="Hello World Page"></p>

                        <p>
                            OK! So now we've got our router routing us to our view, which we are
                            viewing. Let's do some modeling. We'll give you a moment if you need to fix
                            your hair or makeup.
                        </p>

                        <h3>Part 3 – Create our DB and read stuff from it</h3>


                        <h4>Step 1 and 2 – Run mongod and mongo</h4>

                        Your EC2 already has the MongoDB installed and running. Next, we  need to find 
                        the password for 'root'  for the MonoDB on the EC2 instance. 

                        This password can be viewed in the "System Log" of the Bitnami EC2 instance, as follows:

                        <ul>
                            <li> Select your instance in your EC2 instances page  </li>
                            <li>Select the "Actions ->Get System Log" menu item.
                                <img src='https://wiki.bitnami.com/@api/deki/files/1460/=app-creds-2.png?size=webview'/> </li>
                            <li> Review the system log until you find the application password. 
                                <img src="https://wiki.bitnami.com/@api/deki/files/1461/=app-creds-3.png?size=webview" /> 
                                <div>
                                    IMPORTANT: This password is only shown the first time you start the image. Please save your password in a safe place. We also recommend changing it in your application to a different value.
                                </div>
                            </li>
                        </ul>

                        <p>
                            Let's execute the following command to launch a MongoDB shell so we can do some database work:
                        </p>

                        <h5>Command: bitnami@ip-172-31-20-96:~$</h5>
                        <pre style="" class="prettyprint prettyprinted"> bitnami@ip-172-31-20-96:~$<span class="pln">sudo mongo admin -u root -p <i> your_password</i></span></pre>

                        <p>
                            You'll see something like the following:
                        </p>
                        <pre>MongoDB shell version: 3.0.6
connecting to: /opt/bitnami/mongodb/tmp/mongodb-27017.sock:27017/admin
Server has startup warnings:
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten]
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten]
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2015-11-10T02:39:32.920+0000 I CONTROL  [initandlisten]

    


                        </pre>

                        <p>
                            Additionally, if you're paying attention to your mongod instance, you'll
                            see it mentions that a connection has been established. All right, you've
                            got MongoDB up and running, and you've connected to it with the client.
                            We'll use this client to manually work on our database, for a bit, but
                            it's not necessary for running the website. Only the server daemon
                            (mongod) is needed for that.
                        </p>

                        <h4>Step 3 – Create a Database</h4>

                        <p>

                            Don't worry about "connecting to: ...admin". That is the admin DB used by root.
                            We'll make a new one called "proj1" and create a new user for it.
                            In your Mongo console, type the following:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd"> > use</span><span class="pln"> proj1</span></pre>

                        <p>
                            Now we're using the database "proj1." Nothing
                            actually exists yet. To make the database exist, we have to add some
                            data. We're going to start off by doing that right inside of the Mongo
                            client.
                        </p>

                        <p >  Let's create <a name="pass" style="color:blue"> user/password </a>
                            to database before we can connect it from the code </p> 
                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"> 
   
   > db.createUser( { user: "mean-ec2",
              pwd: "Moon1234",
              roles: [ "readWrite", "dbAdmin" ]
            } )
                        </pre>

                        <h4>Step 4 – Add some Data</h4>

                        <p>
                            One favorite thing about MongoDB is that it uses JSON for its structure,
                            which means it was instantly familiar for us. If you're not familiar with
                            JSON, you'll need to do some reading, as  we are  afraid that's outside the
                            scope of this tutorial.
                        </p>

                        <p>
                            Let's add a record to our collection. For the purposes of this tutorial,
                            we're just going to have a simple database of usernames and email
                            addresses. Our data format will thus look like this:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pun">{</span><span class="pln">
    </span><span class="str">"_id"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">1234</span><span class="pun">,</span><span class="pln">
    </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"cwbuecheler"</span><span class="pun">,</span><span class="pln">
    </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"cwbuecheler@nospam.com"</span><span class="pln">
</span><span class="pun">}</span></pre>

                        <p>
                            You can create your own _id assignment if you really want, but it's
                            best to let Mongo just do its thing. It will provide a unique identifier
                            for every single top-level collection entry. Let's add one and see how it
                            works. In your Mongo shell, type this:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">>db</span><span class="pun">.</span><span class="pln">usercollection</span><span class="pun">.</span><span class="pln">insert</span><span class="pun">({</span><span class="pln"> </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1@testdomain.com"</span><span class="pln"> </span><span class="pun">})</span></pre>

                        <p>
                            Something important to note here: that "db" stands for our database, which
                            as mentioned above we've defined as "proj1". The "usercollection" part is
                            our collection. Note that there wasn't a step where we created the
                            "usercollection" collection. That's because the first time we add to it, it's
                            going to be auto-created. Handy. OK, Hit enter. Assuming everything went
                            right, you should see … nothing. That's not very exciting, so type
                            this:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">&gt;db</span><span class="pun">.</span><span class="pln">usercollection</span><span class="pun">.</span><span class="pln">find</span><span class="pun">().</span><span class="pln">pretty</span><span class="pun">()</span></pre>

                        <p>
                            In case you're curious, the .pretty() method gives us linebreaks. It will
                            return:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pun">{</span><span class="pln">
    </span><span class="str">"_id"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">ObjectId</span><span class="pun">(</span><span class="str">"5202b481d2184d390cbf6eca"</span><span class="pun">),</span><span class="pln">
    </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1"</span><span class="pun">,</span><span class="pln">
    </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1@testdomain.com"</span><span class="pln">
</span><span class="pun">}</span></pre>

                        <p>
                            Except, of course, your ObjectID will be different, since as mentioned,
                            Mongo is automatically generating those. That's all there is to writing
                            to MongoDB from the client app, and if you've ever worked with JSON
                            services before, you are probably going "oh, wow, that's going to be easy
                            to implement on the web." … you're right!
                        </p>

                        <p>
                            A quick note on DB structure: obviously in the long run you're unlikely
                            to be storing everything at the top level. There are a ton of resources
                            on the internet for schema design in MongoDB. Google is your friend!
                        </p>

                        <p>
                            Now that we've got one record, let's add a a couple more. In your Mongo
                            console, type the following:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">&gt;newstuff </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[{</span><span class="pln"> </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser2"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser2@testdomain.com"</span><span class="pln"> </span><span class="pun">},</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser3"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser3@testdomain.com"</span><span class="pln"> </span><span class="pun">}]</span><span class="pln">
&gt;db</span><span class="pun">.</span><span class="pln">usercollection</span><span class="pun">.</span><span class="pln">insert</span><span class="pun">(</span><span class="pln">newstuff</span><span class="pun">);</span></pre>

                        <p>
                            Note that, yes, we can pass an array with multiple objects to our
                            collection. Handy! Another use of db.usercollection.find().pretty() will
                            show all three records:
                        </p>

                        <h5>Mongo Console</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pun">{</span><span class="pln">
        </span><span class="str">"_id"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">ObjectId</span><span class="pun">(</span><span class="str">"5202b481d2184d390cbf6eca"</span><span class="pun">),</span><span class="pln">
        </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1"</span><span class="pun">,</span><span class="pln">
        </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser1@testdomain.com"</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
        </span><span class="str">"_id"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">ObjectId</span><span class="pun">(</span><span class="str">"5202b49ad2184d390cbf6ecb"</span><span class="pun">),</span><span class="pln">
        </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser2"</span><span class="pun">,</span><span class="pln">
        </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser2@testdomain.com"</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
        </span><span class="str">"_id"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="typ">ObjectId</span><span class="pun">(</span><span class="str">"5202b49ad2184d390cbf6ecc"</span><span class="pun">),</span><span class="pln">
        </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser3"</span><span class="pun">,</span><span class="pln">
        </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">"testuser3@testdomain.com"</span><span class="pln">
</span><span class="pun">}</span></pre>

                        <p>
                            Now we're going to start actually interacting with the web server and site
                            that we set up earlier.
                        </p>

                        <h4>Step 5 – Hook Mongo up to Node</h4>

                        <p>
                            This is where the rubber meets the road. In order to connect to Mongo database,
                            we need to install a module called Monk:
                        </p>

                        <h5>Command bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">npm install monk --save </span></pre>
                        <p>
                            The –save switch here instructs NPM to add this dependency in your package.json. The benefit of this is that if you push this application to a repository, and someone else checks it out (or it could be yourself at some other time), all dependencies of your application are referenced in package.json. Then, all you have to do is simply run npm install, and this will automatically
                            install all the referenced modules. This is exactly what we did earlier once we generated our application skeleton.
                        </p>

                        <p>    Let's start by building a page
                            that just spits out our DB entries in a mildly pretty form. Here's the
                            HTML we're shooting to generate:
                        </p>

                        <pre style="" class="prettyprint prettyprinted"><span class="tag">&lt;ul&gt;</span><span class="pln">
    </span><span class="tag">&lt;li&gt;&lt;a</span><span class="pln"> </span><span class="atn">href</span><span class="pun">=</span><span class="atv">"mailto:testuser1@testdomain.com"</span><span class="tag">&gt;</span><span class="pln">testuser1</span><span class="tag">&lt;/a&gt;&lt;/li&gt;</span><span class="pln">
    </span><span class="tag">&lt;li&gt;&lt;a</span><span class="pln"> </span><span class="atn">href</span><span class="pun">=</span><span class="atv">"mailto:testuser2@testdomain.com"</span><span class="tag">&gt;</span><span class="pln">testuser2</span><span class="tag">&lt;/a&gt;&lt;/li&gt;</span><span class="pln">
    </span><span class="tag">&lt;li&gt;&lt;a</span><span class="pln"> </span><span class="atn">href</span><span class="pun">=</span><span class="atv">"mailto:testuser3@testdomain.com"</span><span class="tag">&gt;</span><span class="pln">testuser3</span><span class="tag">&lt;/a&gt;&lt;/li&gt;</span><span class="pln">
</span><span class="tag">&lt;/ul&gt;</span></pre>

                        <p>
                            We know this isn't rocket science, but that's the point. We're just doing
                            a simple DB read-and-write in this tutorial, not trying to build a whole
                            website. First things first, we need to add a few lines to our main
                            app.js file – the heart and soul of our app – in order to
                            actually connect to our MongoDB instance. Open<i> ~/proj1/app.js </i>
                            and at the top you'll see:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'express'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'path'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> favicon </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'serve-favicon'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'morgan'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> cookieParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'cookie-parser'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> bodyParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'body-parser'</span><span class="pun">);</span></pre>

                        <p>
                            Now add these three lines:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'express'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'path'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> favicon </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'serve-favicon'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'morgan'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> cookieParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'cookie-parser'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> bodyParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'body-parser'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// New Code</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> mongo </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'mongodb'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> monk </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'monk'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> db </span><span class="pun">=</span><span class="pln"> monk</span><span class="pun">(</span><span class="str">'mean-ec2:Moon1234@localhost:27017/proj1'</span><span class="pun">);</span></pre>

                        <p>
                            These lines tell our app we want to talk to MongoDB, we're going to use
                            Monk to do it, and our database is located at 'localhost:27017/proj1'. 
                            The user is 'mean-ec2' and the password is 'Moon1234'. We created these 
                            when we were <a href="#pass"> here </a>.
                            Note that '27017' is the default port your MongoDB instance should be
                            running on. If for some reason you've changed it, obviously use that port
                            instead. Now look at the bottom of the file, where you have this:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="pln">app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="pln"> routes</span><span class="pun">);</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/users'</span><span class="pun">,</span><span class="pln"> users</span><span class="pun">);</span></pre>

                        <p>
                            We need to do some work here. Those app.use statements (along with the
                            others you'll find in app.js) are establishing middleware for Express. The
                            short, simple explanation is: they're providing custom functions that the
                            rest of your app can make use of.  It's pretty straightforward, but due to
                            chaining it needs to come <i>before</i> our route definitions, so that
                            they can make use of it.
                        </p>

                        <p>
                            <strong>Above</strong> the two lines just mentioned, add the following:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">// Make our db accessible to our router</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln">res</span><span class="pun">,</span><span class="kwd">next</span><span class="pun">){</span><span class="pln">
    req</span><span class="pun">.</span><span class="pln">db </span><span class="pun">=</span><span class="pln"> db</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">next</span><span class="pun">();</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            <strong style="color:#C00;">NOTE: If you don't put this above the routing
                                stuff mentioned above (app.use('/', routes);), your app WILL NOT
                                WORK.</strong>
                        </p>

                        <p>
                            We already defined "db" when we added Mongo and Monk to app.js. It's our
                            Monk connection object. By adding this function to app.use, we're adding
                            that object to every HTTP request (i.e.: "req") our app makes. Note: this is
                            probably sub-optimal for performance but, again, we're going quick-n-dirty
                            here.
                        </p>

                        <p>
                            So, again, that code needs to go above our routing code. Your entire
                            app.js should look like this, now:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$ ~/proj1/app.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">var</span><span class="pln"> express </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'express'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> path </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'path'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> favicon </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'serve-favicon'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> logger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'morgan'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> cookieParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'cookie-parser'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> bodyParser </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'body-parser'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// New Code</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> mongo </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'mongodb'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> monk </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'monk'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> db </span><span class="pun">=</span><span class="pln"> monk</span><span class="pun">(</span><span class="str">'mean-ec2:Moon1234@localhost:27017/proj1'</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">var</span><span class="pln"> routes </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'./routes/index'</span><span class="pun">);</span><span class="pln">
</span><span class="kwd">var</span><span class="pln"> users </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">require</span><span class="pun">(</span><span class="str">'./routes/users'</span><span class="pun">);</span><span class="pln">

</span><span class="kwd">var</span><span class="pln"> app </span><span class="pun">=</span><span class="pln"> express</span><span class="pun">();</span><span class="pln">

</span><span class="com">// view engine setup</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'views'</span><span class="pun">,</span><span class="pln"> path</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="pln">__dirname</span><span class="pun">,</span><span class="pln"> </span><span class="str">'views'</span><span class="pun">));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">set</span><span class="pun">(</span><span class="str">'view engine'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'jade'</span><span class="pun">);</span><span class="pln">

</span><span class="com">// uncomment after placing your favicon in /public</span><span class="pln">
</span><span class="com">//app.use(favicon(__dirname + '/public/favicon.ico'));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">logger</span><span class="pun">(</span><span class="str">'dev'</span><span class="pun">));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">bodyParser</span><span class="pun">.</span><span class="pln">json</span><span class="pun">());</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">bodyParser</span><span class="pun">.</span><span class="pln">urlencoded</span><span class="pun">({</span><span class="pln"> extended</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">false</span><span class="pln"> </span><span class="pun">}));</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">cookieParser</span><span class="pun">());</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="pln">express</span><span class="pun">.</span><span class="kwd">static</span><span class="pun">(</span><span class="pln">path</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="pln">__dirname</span><span class="pun">,</span><span class="pln"> </span><span class="str">'public'</span><span class="pun">)));</span><span class="pln">

</span><span class="com">// Make our db accessible to our router</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln">res</span><span class="pun">,</span><span class="kwd">next</span><span class="pun">){</span><span class="pln">
    req</span><span class="pun">.</span><span class="pln">db </span><span class="pun">=</span><span class="pln"> db</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">next</span><span class="pun">();</span><span class="pln">
</span><span class="pun">});</span><span class="pln">

app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/'</span><span class="pun">,</span><span class="pln"> routes</span><span class="pun">);</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="str">'/users'</span><span class="pun">,</span><span class="pln"> users</span><span class="pun">);</span><span class="pln">

</span><span class="com">/// catch 404 and forwarding to error handler</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> err </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Error</span><span class="pun">(</span><span class="str">'Not Found'</span><span class="pun">);</span><span class="pln">
    err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">=</span><span class="pln"> </span><span class="lit">404</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">next</span><span class="pun">(</span><span class="pln">err</span><span class="pun">);</span><span class="pln">
</span><span class="pun">});</span><span class="pln">

</span><span class="com">/// error handlers</span><span class="pln">

</span><span class="com">// development error handler</span><span class="pln">
</span><span class="com">// will print stacktrace</span><span class="pln">
</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">app</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'env'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">===</span><span class="pln"> </span><span class="str">'development'</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        res</span><span class="pun">.</span><span class="pln">status</span><span class="pun">(</span><span class="pln">err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">||</span><span class="pln"> </span><span class="lit">500</span><span class="pun">);</span><span class="pln">
        res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'error'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            message</span><span class="pun">:</span><span class="pln"> err</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln">
            error</span><span class="pun">:</span><span class="pln"> err
        </span><span class="pun">});</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

</span><span class="com">// production error handler</span><span class="pln">
</span><span class="com">// no stacktraces leaked to user</span><span class="pln">
app</span><span class="pun">.</span><span class="kwd">use</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">next</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">status</span><span class="pun">(</span><span class="pln">err</span><span class="pun">.</span><span class="pln">status </span><span class="pun">||</span><span class="pln"> </span><span class="lit">500</span><span class="pun">);</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'error'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        message</span><span class="pun">:</span><span class="pln"> err</span><span class="pun">.</span><span class="pln">message</span><span class="pun">,</span><span class="pln">
        error</span><span class="pun">:</span><span class="pln"> </span><span class="pun">{}</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span><span class="pln">

</span><span class="kwd">module</span><span class="pun">.</span><span class="pln">exports </span><span class="pun">=</span><span class="pln"> app</span><span class="pun">;</span></pre>

                        <p>
                            Next thing we need to do is modify our route so that we can actually show
                            data that's held in our database, using our db object.
                        </p>

                        <h4>Step 6 – Pull your data from Mongo and display it</h4>

                        <p>
                            Open up bitnami@ip-172-31-20-96:$~/proj1/routes/index.js in your editor. It's still got
                            the index route, and the goofy /helloworld route. Let's add a third:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/routes/index.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">/* GET Userlist page. */</span><span class="pln">
router</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/userlist'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> db </span><span class="pun">=</span><span class="pln"> req</span><span class="pun">.</span><span class="pln">db</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> collection </span><span class="pun">=</span><span class="pln"> db</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'usercollection'</span><span class="pun">);</span><span class="pln">
    collection</span><span class="pun">.</span><span class="pln">find</span><span class="pun">({},{},</span><span class="kwd">function</span><span class="pun">(</span><span class="pln">e</span><span class="pun">,</span><span class="pln">docs</span><span class="pun">){</span><span class="pln">
        res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'userlist'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="str">"userlist"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> docs
        </span><span class="pun">});</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            OK … that's getting fairly complicated. All it's really doing,
                            though, is extracting the "db" object we passed to our http request, and
                            then using that db connection to fill our "docs" variable with database
                            documents, ie: user data. Then we do a page render just like the other two
                            "gets" in this route file.
                        </p>

                        <p>
                            Basically, we tell our app which collection we want to use
                            ('usercollection') and do a find, then return the results as the variable
                            "docs". Once we have those documents, we then do a render of userlist
                            (which will need a corresponding Jade template), giving it the userlist
                            variable to work with, and passing our database documents to that
                            variable.
                        </p>

                        <p>
                            Next let's set up our Jade template. Navigate to 
                            <i>bitnami@ip-172-31-20-96:$~/proj1/views/</i>
                            and open <i>index.jade</i>. Once again, <strong>immediately save it as </strong>
                            <i>userlist.jade</i>. Then edit the HTML so it looks like this:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/views/userlist.jade</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">extends</span><span class="pln"> layout

block content
    h1</span><span class="pun">.</span><span class="pln">
        </span><span class="typ">User</span><span class="pln"> </span><span class="typ">List</span><span class="pln">
    ul
        each user</span><span class="pun">,</span><span class="pln"> i </span><span class="kwd">in</span><span class="pln"> userlist
            li
                a</span><span class="pun">(</span><span class="pln">href</span><span class="pun">=</span><span class="str">"mailto:#{user.email}"</span><span class="pun">)=</span><span class="pln"> user</span><span class="pun">.</span><span class="pln">username</span></pre>

                        <p>
                            This is saying that we're going to pull in the set of documents we just
                            called userlist over in the route file, and then for each entry (named
                            'user' during the loop), get the email and username values from the object
                            and put them into our html. We've also got the count – i –
                            handy in case we need it, though in this instance we don't.
                        </p>


                        <p>
                            We're all set. Save that file, and let's restart our node server. Remember
                            how to do that? Go to your command prompt, head for<i> bitnami@ip-172-31-20-96:~/proj1$ </i> and
                            ctrl-c to kill your server if it's still running from way back before.
                            Then type:
                        </p>

                        <h5>Command bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted">npm start</pre>


                        <p>
                            Now open your browser and head to
                            <i>http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1/userlist</i>
                            and marvel at the results.
                        </p>

                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot3.png" alt="DB Ouput Page - Default Data"></p>


                        <p>
                            You're now pulling data from the DB and spitting it out onto a web page. Nice!
                        </p>
                        <!--
                        <div class="tip">
                            <h4>Tip</h4>
                            <p>
                                Getting the following error?<br>
                                <em>Cannot read property 'length' of undefined</em> on the line <em>each user, i in userlist</em>?
                            </p>
                            <p class="help"><a href="#" class="expand" rel="tip0002">Click Here for Help</a></p>
                            <div class="more" id="tip0002">
                                <p>
                                    This is a super-common problem people run into. Here are the three most common fixes:
                                </p>
                                <ol>
                                    <li>Check your userlist.jade indents <strong>very carefully</strong>
                         (We suggest checking against the file in the github repo). If they're 
                        even slightly off, it'll cause this error. We t's essential that 
                        userlist.jade look like this:
                        <pre>ul
                            each user, i in userlist
                                li
                                    a(href="mailto:#{user.email}")= user.username
                        </pre>
                                    If any of those indents are incorrect (for example, if the 
                        li is on the same indent level as the "each" line), the whole thing 
                        breaks.
                                    </li>
                                    <li>Make sure you are running mongod.exe <em>in a separate command prompt</em> at the same time you are running your node app. Both must be running in order to connect to the DB.</li>
                                    <li>In app.js instead of localhost use var db = monk('mean-ec2:Moon1234@127.0.0.1:27017/proj1');</li>
                                </ol>
                            </div>
                        </div>
                        -->
                        <p>
                            There one more thing we badly wanted to cover in this tutorial, but because
                            it's already about as long as the Bible,  We are  going to breeze through it
                            here. You could very easily change your userlist view from an
                            Express-driven web page complete with Jade template to a plain old JSON
                            response. You could then access this with AJAX and manipulate it on the
                            client-side, with jQuery for example, instead of on the server side. In
                            fact, We wanted to cover that so badly that We wrote an entire second
                            tutorial on it. You can find the link at the end of this one!
                        </p>

                        <p>
                            Let's finish this up.
                        </p>

                        <h3>Part 4 – The holy grail: writing to the DB</h3>

                        <p>
                            Writing to the database is not particularly difficult. Essentially we need
                            to set up a route that takes a POST, rather than a GET.
                        </p>

                        <h4>Step 1 – Create your data input</h4>

                        <p>
                            We're going quick and dirty here: two ugly, unstyled text inputs and a
                            submit button. 1996-style, but before we get to that, we're going to
                            do some javacripting. Let's start by quickly wiring up a route for our
                            add user form. Open ~/proj1/routes/index.js and add the following code
                            <strong>above</strong> the last module.exports line:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/routes/index.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">/* GET New User page. */</span><span class="pln">
router</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'/newuser'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    res</span><span class="pun">.</span><span class="pln">render</span><span class="pun">(</span><span class="str">'newuser'</span><span class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Add New User'</span><span class="pln"> </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            Now we just need a template. Open up ~/proj1/views/index.jade, save it as
                            newuser.jade, and replace the whole file contents with this:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/views/newuser.jade</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="kwd">extends</span><span class="pln"> layout

block content
    h1</span><span class="pun">=</span><span class="pln"> title
    form</span><span class="com">#formAddUser(name="adduser",method="post",action="adduser")</span><span class="pln">
        input</span><span class="com">#inputUserName(type="text", placeholder="username", name="username")</span><span class="pln">
        input</span><span class="com">#inputUserEmail(type="text", placeholder="useremail", name="useremail")</span><span class="pln">
        button</span><span class="com">#btnSubmit(type="submit") submit</span></pre>

                        <p>
                            Here we're creating a form with the ID "formAddUser" (We like to preface
                            our IDs with the type of thing we're ID'ing. It's a personal quirk). Method
                            is post, action is adduser. Pretty straightforward. Under that we've
                            defined our two inputs and our button.
                        </p>

                        <p>
                            If you restart your node server and go to
                            <i>http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1/newuser</i>
                            you'll see your form in all its glory.
                        </p>

                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot4.png" alt="Welcome to Express Page"></p>

                        <p>
                            Go ahead and submit. Enjoy the 404 error. We're about to fix that.
                        </p>

                        <h4>Step 2 – Create your DB functions</h4>

                        <p>
                            OK, this is pretty much the same process as with the user list.  We don't
                            need another app.use statement since we've already wrapped our database
                            object into every request (see above). That means it'll be accessible to
                            any new routes we write. That means that all we need to do is add a route
                            for POSTing to /adduser.
                        </p>

                        <p>
                            Go back to ~/proj1/routes/index.js and let's create our insertion function. Once
                            again, you'll want to put this <strong>above</strong> the final
                            module.exports line (it doesn't REALLY matter, but it makes things
                            cleaner to wrap up with the export). This is a big one, so we've commented
                            the code pretty thoroughly. Here it is:
                        </p>

                        <h5>bitnami@ip-172-31-20-96:$~/proj1/routes/index.js</h5>
                        <pre style="" class="prettyprint prettyprinted"><span class="com">/* POST to Add User Service */</span><span class="pln">
router</span><span class="pun">.</span><span class="pln">post</span><span class="pun">(</span><span class="str">'/adduser'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">(</span><span class="pln">req</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

    </span><span class="com">// Set our internal DB variable</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> db </span><span class="pun">=</span><span class="pln"> req</span><span class="pun">.</span><span class="pln">db</span><span class="pun">;</span><span class="pln">

    </span><span class="com">// Get our form values. These rely on the "name" attributes</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> userName </span><span class="pun">=</span><span class="pln"> req</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">username</span><span class="pun">;</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> userEmail </span><span class="pun">=</span><span class="pln"> req</span><span class="pun">.</span><span class="pln">body</span><span class="pun">.</span><span class="pln">useremail</span><span class="pun">;</span><span class="pln">

    </span><span class="com">// Set our collection</span><span class="pln">
    </span><span class="kwd">var</span><span class="pln"> collection </span><span class="pun">=</span><span class="pln"> db</span><span class="pun">.</span><span class="kwd">get</span><span class="pun">(</span><span class="str">'usercollection'</span><span class="pun">);</span><span class="pln">

    </span><span class="com">// Submit to the DB</span><span class="pln">
    collection</span><span class="pun">.</span><span class="pln">insert</span><span class="pun">({</span><span class="pln">
        </span><span class="str">"username"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> userName</span><span class="pun">,</span><span class="pln">
        </span><span class="str">"email"</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> userEmail
    </span><span class="pun">},</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">err</span><span class="pun">,</span><span class="pln"> doc</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">err</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="com">// If it failed, return error</span><span class="pln">
            res</span><span class="pun">.</span><span class="pln">send</span><span class="pun">(</span><span class="str">"There was a problem adding the information to the database."</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            </span><span class="com">// And forward to success page</span><span class="pln">
            res</span><span class="pun">.</span><span class="pln">redirect</span><span class="pun">(</span><span class="str">"userlist"</span><span class="pun">);</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
    </span><span class="pun">});</span><span class="pln">
</span><span class="pun">});</span></pre>

                        <p>
                            Obviously in the real world you would want a <em>ton</em> more validating,
                            error-checking, and the like. You'd want to check for duplicate usernames
                            and emails, for example. And to vet that the email address at least looks
                            like a legit entry. But this'll work for now. As you can see, on
                            successfully adding to the DB, we forward the user back to the userlist
                            page, where they should see their newly added user.
                        </p>

                        <p>
                            Are there smoother ways to do this? Absolutely. We're staying as
                            bare-bones as possible here. Now, let's go add some data!
                        </p>

                        <h4>Step 3 – Connect and add data to your DB</h4>

                        <p>
                            <strong>Make sure mongod is running!</strong> Then head to your command
                            prompt, kill your node server if it's still running, and restart it:
                        </p>

                        <h5>Command bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted">npm start</pre>

                        <p>
                            Assuming your server is running, which it should be, return to your web
                            browser and point it at
                            <i>http://ec2-52-24-196-1099.us-west-2.compute.amazonaws.com/proj1/newuser </i>
                            again. There's our exciting form, just like before. Except now let's fill
                            in some values before we hit submit.
                        </p>



                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot5.png" alt="Welcome to Express Page"></p>

                        <p>
                            Click submit, and check it out ... we're back at /userlist and there's
                            our new entry!
                        </p>


                        <p class="browsershot"><img src="Tutorial%20-%20Getting%20Started%20Withmean-cloud_files/browsershot6.png" alt="Welcome to Express Page"></p>

                        <p>
                            You can disconnect your ssh session and the app will keep running. For newbie only, 
                            if you want to kill the Node app later, open a ssh session and  do 
                        </p>
                        <h5>Command bitnami@ip-172-31-20-96:~/proj1$</h5>
                        <pre style="" class="prettyprint prettyprinted">killall -9 .node.bin</pre>

                        <p>
                            We are officially reading and writing from a MongoDB database using
                            Node.js, Express, and Jade. You are now what the kids call a "full stack"
                            developer (probably not a GOOD one, just yet, but We didn't promise that).
                        </p>

                        <p>
                            Congratulations. Seriously. If you followed this all the way through,
                            and if you really paid attention to what you were doing and didn't just
                            paste code, you should have a really solid grasp on routes and views,
                            reading from the DB, and posting to the DB. That is <strong>all you
                                need</strong> to get started developing whatever app you want to build.
                            We don't know about you, but We think that's really cool.
                        </p>

                        <h3>Part 5 – Next steps</h3>

                        <p>
                            From here, there's a million different directions you could go. You could
                            check out
                            <a href="http://mongoosejs.com/" target="_blank">Mongoose</a>,
                            which is another Mongo-manipulation package for Node/Express. I's bigger
                            than Monk, but it also does more. You could check out Stylus, the CSS
                            preprocessor that comes with Express. You could google "Node Express
                            Mongo Tutorial" and see what comes up. Just keep exploring and keep
                            building!
                        </p>

                        <p>
                            We hope this tutorial's been helpful. We wrote it because we could have used
                            it when we got started, and we couldn't seem to find something that was
                            quite at this level, or that broke things down in such long, long,
                            <em>loooong</em> detail. If you made it this far, thanks for sticking
                            with it!
                        </p>

                        <p>
                            <strong>NEW:</strong> The "sequel" to this tutorial is now available! Find it at: <a href="http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/">Creating a Simple RESTful Web App with Node.js, Express, and MongoDB</a>.
                        </p>

                        <p>
                            Resource: the folks at Udemy have put up an incredibly extensive <a href="https://blog.udemy.com/node-js-tutorial/" target="_blank">MEAN stack</a> introduction that's well worth checking out.
                        </p>

                        <h4>Merci Beaucoup</h4>

                        <p>
                            We owe a huge debt of thanks to the following code-slingers whose
                            tutorials, slides, presentations, videos, and tweets helped me to figure
                            all of this stuff out. You guys rock!
                        </p>

                        <ul>
                            <li>
                                <a href="http://cwbuecheler.com/" target="_blank">Christopher Buecheler</a>, whose article is what this article is based on 
                                <a href="http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/" target="_blank">Tutorial - Getting Started With Node.js, Express, MongoDB </a> 
                            </li>
                            <li>
                                <a href="http://rckbt.me/" target="_blank">Raquel "Rockbot" Velez</a> (<a href="https://twitter.com/rockbot" target="_blank">@rockbot</a>), whose video presentation <a href="http://www.youtube.com/watch?v=icNHLlRazds" target="_blank">"Front-End Development in Node.js"</a> from <a href="http://events.jquery.org/" target="_blank">jqConf Portland</a>
                                got me interested enough to finally dig my thumbs into Node. She is a 
                                talented web developer, gives a fun talk, and also writes code for 
                                robots!
                            </li>
                            <li>
                                <a href="https://twitter.com/azat_co" target="_blank">Azat Mardanov</a>, whose article <a href="http://webapplog.com/intro-to-express-js-simple-rest-api-app-with-monk-and-mongodb/" target="_blank">"Intro to Express.js: Simple REST APWe app with Monk and MongoDB"</a> helped me a ton with establishing a basic understanding of how all of these different pieces talk to each other.
                            </li>
                            <li>
                                <a href="https://twitter.com/ijayson66" target="_blank">Jason Everett</a>, whose article <a href="http://blog.ijasoneverett.com/2013/03/a-sample-app-with-node-js-express-and-mongodb-part-1/" target="_blank">"A Sample App with Node.js, Express and MongoDB – Part 1"</a> helped me pick up some important knowledge.</li>
                            <li>
                                <a href="https://twitter.com/ciaran_j" target="_blank">Ciaran Jessup</a>, whose article <a href="http://howtonode.org/express-mongodb" target="_blank">"Blog rolling with mongoDB, express and Node.js"</a> over at <a href="http://howtonode.org/" target="_blank">How To Node</a> was another good resource!
                            </li>
                        </ul>
                        <!--
                        <h4>About the author</h4>
                        
                        
                        <p>
                            <a href="http://cwbuecheler.com/">Christopher Buecheler</a> is an autodidact polymath, which is an <em>incredibly</em> pretentious way of saying that he's a jack of all trades who didn't like college. By day he's a <a href="http://cwbuecheler.com/web/">front-end developer</a> for a San Francisco startup. By night <a href="http://cwbuecheler.com/writing/">he's a popular novelist</a>, with four books released. He also is an <a href="http://www.huffingtonpost.com/tony-sachs/a-cocktail-safari-with-ta_b_867015.html" target="_blank">award-winning amateur mixologist</a> who <a href="http://cwbuecheler.com/writing/#nonfiction">writes cocktail articles for Primer Magazine</a> and <a href="http://drinkshouts.com/" target="_blank">runs a cocktail blog</a>,
                         and he brews beer on occasion. He follows the NBA avidly and the NFL 
                        casually (and sometimes glances at MLB). He lives in Providence, Rhode 
                        Island, with his awesome French wife and their two cats. He is trying to
                         learn French but wishes he could just download it from the Matrix.
                        </p>
                        
                            
                            <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
                            
                        -->


                    </article>
                    <!-- /CONTENT -->

                </div>
                <!-- /CONTENT & SIDEBAR WRAP -->





            </div>
            <!-- /BASE CONTAINER -->

        </div>

    </body></html>